package com.guhappy.action;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.guhappy.model.user.WebUser;
import com.guhappy.model.user.WebUserHome;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("default")
@InterceptorRef("auditInterceptorStack")
public class UserLoginAction extends ActionSupport implements SessionAware  {

	private static final long serialVersionUID = 1L;
	SessionMap<String,String> sessionmap;  
	private static final Logger logger = Logger
			.getLogger(UserLoginAction.class);
	private String passwd;

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	private String email;
	
	
	@Action(value= "signOnAction" ,results={
			@Result(name="success",location="/pages/welcome.jsp"),
			@Result(name="input",location="/pages/signOn.jsp")
			})
	public String execute() throws Exception {
		System.out.println("login invoked");
		WebUser webUser = null;

		webUser = WebUserHome.findUser(email,
				HashPassword(passwd));
	
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if (webUser == null) {
			this.addActionError(getText("user_pass_not_matched"));
			return INPUT;
		} else if (webUser.getStatus().equals("0")){
			this.addActionError(getText("check_registration_email"));
			return INPUT;
		}else{
			request.getSession().setAttribute("webUser", webUser);
			return SUCCESS;
		}	
	}
	
	@Action(value= "signOffAction" ,results={
			@Result(name="success",location="/pages/welcome.jsp")
			})
	public String logout(){  
	    sessionmap.invalidate();  
	    return "success";  
	}

	public static String HashPassword(String sPASSWORD) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest
					.getInstance("MD5");
			byte[] array = null;
			try {
				array = md.digest(sPASSWORD.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
			logger.error(e);
		}
		return null;
	}

	public void setSession(Map<String, Object> session) {
		sessionmap=(SessionMap)session; 
	}
}